
struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode()
      : val(0), left(nullptr), right(nullptr){} * TreeNode(int x)
      : val(x),
        left(nullptr), right(nullptr) {}
  TreeNode(int x, TreeNode *left, TreeNode *right)
      : val(x), left(left), right(right) {}
};

void dfs(TreeNode *root, int &res, bool isLeft) {
  if (!root->left && !root->right && isLeft) {
    res += root->val;
    return;
  }
  if (root->left) {
    dfs(root->left, res, true);
  }
  if (root->right) {
    dfs(root->right, res, false);
  }
}
int sumOfLeftLeaves(TreeNode *root) {
  if (!root)
    return 0;
  int sum = 0;
  if (root->left)
    dfs(root->left, sum, true);
  if (root->right)
    dfs(root->right, sum, false);
  return sum;
}